package imdbfolder.gui;

import imdbfolder.app.Application;
import imdbfolder.data.Movie;
import imdbfolder.data.MovieSelectionMap;
import imdbfolder.workers.MovieFinder;
import imdbfolder.workers.MovieInfoFetcher;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutionException;

import jregex.Pattern;

public class UnknownMovieController {

    private UnknownMoviePanel view;

    public UnknownMoviePanel getView() {
        return view;
    }

    private final File file;

    private final MovieListController listController;

    private String searchString;

    public String getSearchString() {
        return searchString;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }

    public File getFile() {
        return file;
    }

    public UnknownMovieController(File file, MovieListController listController) {
        this.file = file;
        this.listController = listController;

        if (file.isFile()) {
            searchString = new Pattern("\\.[^\\.]+$").replacer("").replace(file.getName());
        }
        else {
            searchString = file.getName().replace("."," ").split("[(]",2)[0];
        }
        
        // System.out.println(file + " => " + searchString);

        initialize();
    }

    public void initialize() {
        if (view == null) {
            view = new UnknownMoviePanel(this);
        }

        view.initialize();
    }

    public void runSearch(boolean highPriority, final boolean alwaysUseFirstHit) {
        view.setProcessing();

        MovieFinder finder = new MovieFinder(searchString) {
            @Override
            public void onDone() {
                try {
                    List<Movie> movies = get();

                    if (isExactHit()) {
                        selectMovie(movies.get(0));
                        MovieSelectionMap.getInstance().putMovie(movies.get(0));
                    }
                    else {
                        if (alwaysUseFirstHit && movies.size() > 0) {
                            MovieInfoFetcher movieInfoFetcher = new MovieInfoFetcher(movies.get(0)) {
                                @Override
                                public void onDone() {
                                    try {
                                        selectMovie(get());
                                        MovieSelectionMap.getInstance().putMovie(get());
                                    }
                                    catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    catch (ExecutionException e) {
                                        e.printStackTrace();
                                    }
                                }
                            };

                            Application.runTask(movieInfoFetcher, true);
                        }
                        else {
                            view.setPossibleMovies(movies);
                        }
                    }
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        };

        Application.runTask(finder, highPriority);
    }

    public void selectMovie(Movie movie) {
        movie.setFileName(file.getAbsolutePath());
        listController.selectMovie(file, movie);
    }

    public void setVisible(boolean isMatch) {
        view.setVisible(isMatch);
    }
}
